# -*- coding: utf-8 -*-
import csv
import sys
import random
import Queue
total=0
level=0
currentLevelSize=0
nodeQueue=Queue.Queue()
voltageLevel=['500kv','220kv','110kv','35kv','10kv','220v']
def createRandomNumberChildren(left,right):	
	return random.randint(left,right)
def createChildrenByTotalAndNumber(number,childrenlevel):
	global total
	global nodeQueue	
	childrenLine=""
	for i in xrange(1,number+1):
		childrenLine=childrenLine+voltageLevel[childrenlevel]+"@"+str(total+i)+" "
		nodeQueue.put(voltageLevel[childrenlevel]+"@"+str(total+i))		
	total+=number
	return [childrenLine,number]
def createNextLevel(level,file,left,right):
	global total
	global currentLevelSize	
	tempTotal=0
	for i in xrange(0,currentLevelSize):
		id=nodeQueue.get()
		nextChildren=createChildrenByTotalAndNumber(createRandomNumberChildren(left,right),level+1)
		file.writerow([id,nextChildren[0]])
		tempTotal+=nextChildren[1]
	currentLevelSize=tempTotal
def createLastLevel(level,file):
	global total
	total+=1
	global currentLevelSize	
	for i in xrange(0,currentLevelSize):
		id=nodeQueue.get()		
		file.writerow([id,""])
	currentLevelSize=0
def createCsvRoot(file):
	global level
	global nodeQueue
	global currentLevelSize	
	file.writerow(['name','children'])
	file.writerow([voltageLevel[level]+'@0',createChildrenByTotalAndNumber(createRandomNumberChildren(10,10),level+1)[0]])	
	currentLevelSize=nodeQueue.qsize()
	while(level<len(voltageLevel)-2):
		level+=1
		createNextLevel(level,file,12,12)
	# level+=1
	# createLastLevel(level,file)
if __name__ == '__main__':
	print "test create topo"
	if len(sys.argv)==2:
		file=open(sys.argv[1],'wb+')
		f=csv.writer(file)
		createCsvRoot(f)
		print total
		file.close()
	else:		
		file=open('dataForStorm.csv','wb+')
		f=csv.writer(file)
		createCsvRoot(f)
		print total
		file.close()